package com.zwl.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zwl.dao.BaseDao;
import com.zwl.dao.UserDao;
import com.zwl.po.UserPo;

public class UserDaoImpl extends BaseDao<UserPo> implements UserDao {

	@Override
	public List<UserPo> findByCondition(Map<String, Object> map) {
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from b_user where 1=1 ");
		
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("user_id", "id");
		columnsMap.put("user_id", "id");
		return this.executeQuery3(sf.toString(), columnsMap, UserPo.class, paramList.toArray());
	}

	@Override
	public UserPo findbyId(Integer id) {
		List<Object> paramList=new ArrayList<>();
		String sql=" select * from b_user where user_id=? ";
		paramList.add(id);
		
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("user_id", "id");
		List<UserPo> list=this.executeQuery3(sql, columnsMap, UserPo.class, paramList.toArray());
		return (list!=null && list.size()>0)?list.get(0):null;
	}

	@Override
	public Integer[] saveUser(UserPo entity) {
		String sql="insert into b_user values(seq_menu_id.nextval,?,?,?,?,?,?,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getLoginName());
		paramList.add(entity.getPassword());
		paramList.add(entity.getCreatedId());
		paramList.add(entity.getCreatedTime());
		paramList.add(entity.getUpdatedId());
		paramList.add(entity.getUpdatedTime());
		paramList.add(entity.getIsActive());
		return (Integer[]) this.executeUpdate(sql,"insert",new String[]{"USER_ID"}, paramList.toArray());
	}

	@Override
	public int updateUser(UserPo entity) {
		StringBuffer sf=new StringBuffer();
		sf.append(" update b_user set ");
		sf.append("    login_name=? ");
		sf.append("   ,password=? ");
		sf.append("   ,created_Id=? ");
		sf.append("   ,created_Time=? ");
		sf.append("   ,updated_Id=? ");
		sf.append("   ,updated_Time=? ");
		sf.append("   ,is_active=? ");
		sf.append(" where user_id=? ");
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getLoginName());
		paramList.add(entity.getPassword());
		paramList.add(entity.getCreatedId());
		paramList.add(entity.getCreatedTime());
		paramList.add(entity.getUpdatedId());
		paramList.add(entity.getUpdatedTime());
		paramList.add(entity.getIsActive());
		paramList.add(entity.getId());
		return (Integer) this.executeUpdate(sf.toString(),"update",null, paramList.toArray());
	}
	
	@Override
	public int saveUserRole(Integer userId, Integer roleId) {
		String sql="insert into b_user_role values(?,?) ";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(roleId);
		return (Integer) this.executeUpdate(sql,"update",null, paramList.toArray());
	}
	
	@Override
	public int deleteUserRole(Integer userId, Integer roleId) {
		String sql="delete b_user_role where user_id=? ";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(userId);
		return (Integer) this.executeUpdate(sql,"delete",null, paramList.toArray());
	}

	@Override
	public UserPo getEntity(ResultSet rs) {
		return null;
	}

}
